package com.eascs.web.o2o.web.aging.dao;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.wf.ProcessNodeTemplate;

@Repository
public class ProcessNodeTemplateDao {
	private static Logger log = LoggerFactory.getLogger(ProcessNodeTemplateDao.class);
	private static final String NODE_TEMPLATE_TABLE = "ods_o2oworkflow_node_template";


	@Inject
	JdbcSession jdbcSessionBiods;
	
	public List<ProcessNodeTemplate> getByProcessId(String processId) {
		String sql = "select * from " + NODE_TEMPLATE_TABLE + " where process_no = ? order by creat_time desc";
		List<ProcessNodeTemplate> nodeTemplateList = jdbcSessionBiods.findEntitys(sql, ProcessNodeTemplate.class,
				processId);
		sortNodeTemplate(nodeTemplateList);
		return nodeTemplateList;
	}
	
	public List<ProcessNodeTemplate> getByProcessId(String processId,List<String> excludeNodeCodeList) {
		String sql = "select * from " + NODE_TEMPLATE_TABLE + " where process_no = ? order by order_no ASC";
		List<ProcessNodeTemplate> nodeTemplateList = jdbcSessionBiods.findEntitys(sql, ProcessNodeTemplate.class,processId);
		
		if (excludeNodeCodeList == null || excludeNodeCodeList.size() == 0) {
			return nodeTemplateList;
		}
		
		log.info("start exclude nodeCodeList processId:[{}] ,excludeNodeCodeList:[{}], nodeTemplateList size:[{}]",processId, excludeNodeCodeList.toString(),nodeTemplateList.size());
		Iterator<ProcessNodeTemplate> it=nodeTemplateList.iterator();
		while (it.hasNext()) {
			ProcessNodeTemplate node = it.next();
			for (String excNode : excludeNodeCodeList) {
				if (StringUtils.equals(node.getNodeCode(), excNode)) {
					it.remove();
				}
			}
		}
		log.info("end exclude nodeCodeList processId:[{}] ,excludeNodeCodeList:[{}], nodeTemplateList size:[{}]",processId, excludeNodeCodeList.toString(),nodeTemplateList.size());
		return nodeTemplateList;
	}
	
	
	/**
	 * 流程节点排序
	 * 
	 * @param nodeTemplateList
	 */
	public void sortNodeTemplate(List<ProcessNodeTemplate> nodeTemplateList) {
		Collections.sort(nodeTemplateList, new Comparator<ProcessNodeTemplate>() {
			public int compare(ProcessNodeTemplate o1, ProcessNodeTemplate o2) {
				return o1.getOrderNo() - o2.getOrderNo();
			}
		});
	}

	public List<Object[]> findNodeTemplatesByProcessId(String processId) {
		String sql = "SELECT node_id, node_name, node_desc,node_type,  refer_url,   time_effective,  role_name,order_no,notes_template"
				+ " FROM "+NODE_TEMPLATE_TABLE+" where 1=1 and process_no=? order by order_no";
		return this.jdbcSessionBiods.findObjectsList(sql, processId);
	}
	
	public int findTemplateTimeEffective(String processId) {
		String sql = "SELECT SUM(time_effective) FROM "+NODE_TEMPLATE_TABLE+" WHERE process_no=?";
		Integer count=this.jdbcSessionBiods.findInteger(sql, processId);
		return count == null ? 0 : count;
	}

	public int delById(String nodeId) {
		String sql = "delete from "+NODE_TEMPLATE_TABLE+" where node_id = ?";
		return this.jdbcSessionBiods.execute(sql, nodeId);
	}

}
